<template>
    <div class="chat_container">
        <div class="chat_content">
            <div class="chat_left">
                <img src="../../assets/chartAvator.png" class="logo" />
                <div class="title">爱问小鲸</div>
                <div class="bottom">
                    <div class="bottom_title">注意事项：</div>
                    <div class="bottom_des">1.请提问科普相关知识。</div>
                    <div class="bottom_des">2.请注意内容，不要违法违规。</div>
                </div>
            </div>
            <div class="chat_right">
                <div class="header">爱问小鲸</div>
                <div class="content" ref="contentContainer">
                    <div v-for="(item, index) in messageList" :key="index" :id="'item-' + index"
                        :class="['message-item', item.type === 'user' ? 'message-right' : 'message-left']">
                        <img class="avatar"
                            :src="item.type === 'user' ? touxiang : touxiangs" />
                        <div class="message-content">
                            <span>{{ item.content }}</span>
                        </div>
                    </div>
                </div>
                <div class="bottom">
                    <el-input class="message-input" v-model="inputMessage" placeholder="嗨!  想对小鲸说什么呢"
                        @keyup.enter="sendMessage" />
                    <button class="send-btn" :disabled="loading" :class="{ 'send-btn-disabled': loading }"
                        @click="sendMessage">
                        {{ loading ? '思考中...' : '提问' }}
                    </button>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { callWithMessage,getInfo } from '@/api/index.ts'
import { ref } from 'vue'
const inputMessage = ref('')
const loading = ref(false)
const messageList = ref([
    {
        type: 'ai',
        content: '您好，我是小鲸AI，有什么科普知识不懂的，可以问我啊，我会思考给你回答问题。'
    }
])
const contentContainer = ref()
const touxiang = ref('')
const touxiangs = import.meta.env.VITE_APP_PIC_URL + '/uploads/2025/5/20/1747713990548_a071a6a4/aisss.png?x-oss-process=image/resize,w_300'
const sendMessage = async () => {
    if (!inputMessage.value.trim()) return;
    loading.value = true;  // 开始加载
    // 添加用户消息
    messageList.value.push({
        type: 'user',
        content: inputMessage.value
    });

    await nextTick(() => {
        const container = contentContainer.value;
        container.scrollTop = container.scrollHeight;
    });

    const res = await callWithMessage({ question: inputMessage.value })
    messageList.value.push({
        type: 'ai',
        content: res.data.output.choices[0].message.content
    });
    loading.value = false;

    inputMessage.value = '';

    await nextTick(() => {
        const container = contentContainer.value;
        container.scrollTop = container.scrollHeight;
    });
}

onMounted(async() => { 
    const res = await getInfo()
    touxiang.value = import.meta.env.VITE_APP_PIC_URL + '/' + res.data.user.avatarImage
});

</script>

<style scoped lang="scss">
.chat_container {
    height: calc(100% - 160px);
    margin: 0;
    padding: 0;
    width: 100%;
    background-color: #f5f7fa;
    opacity: 0.8;
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: content-box;
    padding-top: 100px;

    :deep(.el-input__wrapper){
        box-shadow: none !important;
    }
    :deep(.is-focus){
        box-shadow: none !important;
    }

    .chat_content {
        width: 1400px;
        height: calc(100% - 60px);
        margin-top: 30px;
        background: #FFFFFF;
        box-shadow: 0px 2px 7px 0px rgba(0, 0, 0, 0.09);
        border-radius: 20px;
        box-sizing: border-box;
        padding: 48px 104px;
        display: flex;
        align-items: center;

        .chat_right {
            width: 50%;
            height: 100%;
            border: 1px solid #E6E6E6;
            border-radius: 10px;
            box-sizing: border-box;

            .header {
                font-weight: 400;
                font-size: 30px;
                color: #000000;
                text-align: center;
                width: 100%;
                margin: 30px 0;
            }

            .content {
                width: 100%;
                height: calc(100% - 189px);
                overflow: auto;
                box-sizing: border-box;
                padding: 0 33px;


                .message-item {
                    display: flex;
                    margin-bottom: 30px;
                    align-items: center;

                    .avatar {
                        width: 65px;
                        height: 65px;
                        border-radius: 50%;
                    }

                    .message-content {
                        max-width: 70%;
                        margin: 0 20px;
                        padding: 10px 20px;
                        border-radius: 10px;
                        background-color: #f0f0f0;
                        word-break: break-all;
                    }
                }

                .message-left {
                    .message-content {
                        background-color: #f0f0f0;
                    }
                }

                .message-right {
                    flex-direction: row-reverse;

                    .message-content {
                        background-color: #007AFF;
                        color: #fff;
                    }
                }
            }

            .bottom {
                width: 100%;
                height: 89px;
                background: #EFEFEF;
                border-radius: 10px;
                display: flex;
                align-items: center;
                justify-content: space-around;

                .message-input {
                    padding: 0 15px;
                    margin-right: 20px;

                    width: 391px;
                    height: 46px;
                    background: #FFFFFF;
                    border-radius: 23px;
                    border: none;

                    &[disabled] {
                        opacity: 0.7;
                    }
                }

                .send-btn {
                    width: 100px;
                    height: 40px;
                    text-align: center;
                    background-color: #007AFF;
                    color: #fff;
                    border-radius: 10px;
                    border: none;

                    &.send-btn-disabled {
                        background-color: #999;
                        opacity: 0.7;
                    }
                }
            }
        }

        .chat_left {
            width: 50%;
            height: 100%;

            .logo {
                width: 107px;
                height: 120px;
                margin-left: 60px
            }

            .title {
                font-weight: 400;
                font-size: 24px;
                color: #000000;
                margin-left: 60px
            }

            .bottom {
                margin-top: 86px;
                margin-left: 10px;

                .bottom_title {
                    font-size: 18px;
                    color: #000000;
                }

                .bottom_des {
                    color: #A4A4A4;
                    font-size: 14px;
                    margin-left: 20px;
                    margin-top: 20px;
                }
            }
        }
    }
}
</style>